package com.kye.express.business.limit_time_query;

import android.arch.lifecycle.Lifecycle;
import android.content.Context;
import android.text.TextUtils;

import com.kye.addrpicker.model.AddressPickerResult;
import com.kye.express.R;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.RxView;
import com.kye.model.ApiService;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.limit_time_query.RequestFindServiceAging;
import com.kye.model.bean.limit_time_query.ResultFindServiceAging;
import com.kye.model.service.LimitTimeQueryService;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public interface LimitTimeQueryActivityContract {

    interface LimitTimeQueryView extends RxView {

        void onComplete();

        void onLoading();

        void onLoaded(ResultFindServiceAging datas);

        void onError(String msg);
    }

    class LimitTimeQueryViewPresenter extends BasePresenter<LimitTimeQueryView> {

        private Context mContext;

        public LimitTimeQueryViewPresenter(Context context) {
            this.mContext = context;
        }

        /**
         * 请求服务器数据
         */
        public void loadDatas(AddressPickerResult startAddress, AddressPickerResult endAddress, String time) {
            RequestFindServiceAging requestFindServiceAging = new RequestFindServiceAging(startAddress.getProvince(), startAddress.getCity(), startAddress.getArea()
                    , endAddress.getProvince(), endAddress.getCity(), endAddress.getArea(), endAddress.getStreet(), time, "0");
            RetrofitManager.getInstance().createOpenApiService(LimitTimeQueryService.class)
                    .findServiceAging(requestFindServiceAging)
                    .compose(getView().getProvider().<OpenApiResult<ResultFindServiceAging>>bindUntilEvent(Lifecycle.Event.ON_DESTROY))
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((new OpenApiResultObserver<ResultFindServiceAging>() {

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(mContext.getString(R.string.common_net_error));
                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResultFindServiceAging> result, ResultFindServiceAging datas) {
                            getView().onLoaded(datas);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResultFindServiceAging> result, int code, String msg) {
                            if (!TextUtils.isEmpty(msg)) {
                                getView().onError(msg);
                            } else {
                                getView().onError(mContext.getString(R.string.common_net_error));
                            }
                        }

                        @Override
                        public void onLoaded() {
                            getView().onComplete();
                        }
                    }));
        }
    }
}
